import React, { useState, useMemo, useEffect } from 'react';
import { uDb, show_st, ColView, filter_list, useHigh, GridTable, filter_row, useCmd, WaitUmd, MainView, no_del, BtTxt,
    Title, cline, obj_int, InfoDlg, uJob, obj_str, PanelFind, Btn, is_empty, loc } from 'sui';
import { get_col, load_ng, fix_col } from 'test/base/cmd';
import { load_col, load_supply } from '../../stock/base/cmd';
import { useAccess } from 'cache';
import { DlgState } from 'task/ctl';

const SimpleView = ({ mod, submod, title, acc }) => {
    const [open_val, openVal] = useState(false);
    const [open_info, openInfo] = useState(false);
    const [map_supply, mapSupply] = useState();
    const [map_ng, mapNg] = useState();
    const [col, setCol] = useState();
    const [high] = useHigh(140, 100);
    const [no_acc] = useAccess();
    const umd = useCmd(`file/list/${mod}`, null, null, "tech");

    useEffect(() => {
        load_col(mod, setCol);
        load_supply(submod, null, mapSupply);
        load_ng(null, mapNg, mod);
    }, [mod, submod]);

    const getRow = useMemo(() => {
        const lst = filter_list(["sn"], col);
        return filter_row(umd.value, lst, umd.filter);
    }, [umd.value, umd.filter, col]);

    const hreturn = () => {
        load_col(mod, setCol);
        umd.hreturn();
    };

    const pact = {
        "state": () => openVal(true),
        "back": v => {
            const order = obj_str(v, "order");
            if (!order) {
                alert(loc("l_580"));
                return;
            }

            if (no_del(loc("l_577"))) {
                return;
            }

            umd.startWait();

            const cmd = {
                line: cline(),
                k: order,
                sn: obj_str(v, "sn"),
                ngson: obj_str(v, "ngson"),
                path: mod,
                num: obj_int(v, "num"),
            };

            uJob(null, "sale/toback", cmd,
                r => {
                    umd.reload(umd.endWait);
                    openInfo(true);
                },
                e => umd.setErr());
        }
    };

    const act = {
        "state": (v) => {
            umd.startWait();

            const cmd = {
                line: cline(),
                sn: obj_str(umd.cur, "sn"),
                v: {
                    state: obj_str(v, "state")
                }
            };

            uDb(null, `file/md/${mod}`, cmd, r => umd.reload(umd.endWait), e => umd.setErr());
        }
    };

    if (umd.isFin("col")) {
        const list = [
            {sn: mod, name: title, fix: fix_col()},
        ];

        return <ColView hreturn={hreturn} title={title} subtitle={loc("l_221")} list={list} cur={mod}/>;
    }

    const hDel = (d) => {
        if (no_del()) {
            return;
        }

        umd.update(`file/rm/${mod}`, {"sn": obj_str(d, "sn")});
    };

    const getCol = () => {
        if (is_empty(col)) {
            return null;
        }

        const tmp = [
            {name: loc("l_193"), key: 'index', width: 70, frozen: true},
            {name: loc("l_238"), key: 'e_opt', width: 200, frozen: true, formatter: props => {
                const d = props.row;
                return (
                    <div className="inline-flex">
                        <BtTxt hcmd={() => umd.pact(pact, d, "back")} disabled={no_acc(acc) || d.state === "rej"}>{loc("l_767")}</BtTxt>
                        <BtTxt hcmd={() => umd.pact(pact, d, "state")} disabled={no_acc(acc)}>{loc("l_355")}</BtTxt>
                        <BtTxt hcmd={() => hDel(d)} disabled={no_acc(acc)}>{loc("l_237")}</BtTxt>
                    </div>
                );
            }},
        ];

        get_col(col, tmp, map_supply, null, map_ng);

        return tmp;
    };

    const fix_state = () => {
        return [
            {sn: "run", name: show_st("run")},
            {sn: "rej", name: show_st("rej")},
        ];
    };

    const Right = () => (
        <div className="inline-flex">
            <Btn className="mr-4" hcmd={() => umd.pact(null, null, "col")} disabled={no_acc(acc)}>{loc("l_221")}</Btn>
        </div>
    );

    const Left = () => <Title mod={title} sub={loc("l_831")}/>;

    return (
        <MainView>
            <PanelFind left={<Left/>} right={<Right/>} hcmd={v => umd.setFilter(v)}/>
            <GridTable col={getCol()} row={getRow} high={high}/>
            <InfoDlg open={open_info} setOpen={openInfo} msg={loc("l_859")}/>
            <DlgState open={open_val} hcmd={v => umd.act(act, v)} setOpen={openVal} data={umd.cur} lst={fix_state()}/>
            <WaitUmd umd={umd}/>
        </MainView>
    );
};

export {SimpleView};
